/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.ui.dialogs;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.Package;

import com.cea.papyrus.core.PapyrusPlugin;

/**
 * @deprecated see {@link ElementTreeSelectionDialog}
 */
public class NamespaceTreeSelectionDialog extends Dialog{

	/**
	 * Associated Behavior.
	 */
	protected Package selectedPackage;
	protected static final Image IMG_PACKAGE = PapyrusPlugin.getImage(PapyrusPlugin.UML_ICONS_16x16+"Package.gif");
//	protected static final Image IMG_PACKAGE_IMPORT = PapyrusPlugin.getImage(PapyrusPlugin.UML_ICONS_16x16+"PackageImport.gif");

	/**
	 * The selection tree.
	 */
	protected Tree tree;

	private Package model;
	private String typeName ;
	private String metaclassName ;
	
	/** set of package that keeps tracks of all imported packages in the model */
	private Set<Package> importedPackages = new HashSet<Package>();
	
	/**
	 * 
	 * 
	 * @param model 
	 * @param parent 
	 */
	public NamespaceTreeSelectionDialog(Shell parent, Package model, String typeName, String metaclassName){
		super(parent);
		this.model=model;
		this.typeName = typeName ;
		this.metaclassName = metaclassName ;
	}

	@Override
	protected void configureShell(Shell shell) {
		// TODO Auto-generated method stub
		super.configureShell(shell);
		shell.setText("Select owner of " + metaclassName + " " + typeName) ;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.dialogs.ElementImportTreeSelectionDialog#buildImportTreeList(org.eclipse.swt.widgets.TreeItem, org.eclipse.uml2.uml.Package)
	 */
	/**
	 * Creates the dialog area
	 * 
	 * @param parent composite
	 * 
	 * @return control
	 */
	protected Control createDialogArea(Composite parent){
		Composite composite = (Composite)super.createDialogArea(parent);
		tree = new Tree(composite, SWT.SINGLE | SWT.BORDER | SWT.V_SCROLL);

		TreeItem item = new TreeItem(tree, SWT.NONE);
		item.setText(model.getName());
		item.setData(model);
		item.setImage(IMG_PACKAGE);
//		buildImportTreeList(item, model);

		// build basic imported packages list
//		HashSet<Package> importedPackages = LookForElement.buildImportedTopPackagesList(model, new HashSet<Package>());
		
		// extends this list to all imported packages  (import from import, etc...)
//		Iterator<Package> packages = LookForElement.buildExtendedImportedPackagesList(importedPackages, model).iterator();

		for (Iterator i = model.getPackagedElements().iterator(); i.hasNext() ; ) {
			Element e = (Element)i.next() ;
			if (e instanceof Package) {
				Package currentPackage = (Package)e;
				item = new TreeItem(tree, SWT.NONE);
				String name = currentPackage.getName();
				item.setText((name!=null)?name:"<undefined>");
				item.setData(currentPackage);
				item.setImage(IMG_PACKAGE);
				buildPackageTreeList(item, currentPackage);
			}
		}
		tree.setLayoutData(new GridData(300, 300));

		tree.addSelectionListener(new SelectionListener() {
			public void widgetSelected(SelectionEvent e){
				TreeItem[] selection = tree.getSelection();
//				if(selection[0].getData() instanceof Type){
					selectedPackage = (Package)selection[0].getData();
//				}
			}

			public void widgetDefaultSelected(SelectionEvent e) {
			}
		});

		return composite;
	}

	
	/**
	 * This method builds the element tree widget containing all packageable elements of a given package.
	 * @param _package the package to explore
	 * @param elemTree tree parent of packageable elements
	 */
	protected void buildPackageTreeList(TreeItem elemTree, Package _package){
		Iterator elemIter = _package.getPackagedElements().iterator();
		while(elemIter.hasNext()) {
			Element elem = (Element) elemIter.next();
			if(elem instanceof Package){
				TreeItem item = new TreeItem(elemTree, SWT.NONE);
				String name = ((NamedElement)elem).getName();
				item.setText((name!=null)?name:"<undefined>");
				item.setData(elem);
				item.setImage(PapyrusPlugin.UML_LABEL_PROVIDER.getImage(elem));
				buildPackageTreeList(item, (Package)elem);
			}
//			else if(elem instanceof Type){
//				TreeItem item = new TreeItem(elemTree, SWT.NONE);
//				String name = ((NamedElement)elem).getName();
//				item.setText((name!=null)?name:"<undefined>");
//				item.setData(elem);
//				item.setImage(PapyrusPlugin.UML_LABEL_PROVIDER.getImage(elem));
//				if (elem instanceof TemplateableElement) {
//					TemplateableElement templateableElement = (TemplateableElement)elem ;
//					if (templateableElement.isTemplate()) {
//						buildTemplateTreeList(item, templateableElement);
//					}
//				}
//			} 
		}
	}

//	protected void buildTemplateTreeList(TreeItem elemTree, TemplateableElement templateableElement){
//		com.cea.utils.TemplateSignature templateSignatureUtil = new com.cea.utils.TemplateSignature(templateableElement.getOwnedTemplateSignature()) ;
//		Iterator ite= templateSignatureUtil.getAccessibleTypes().iterator();
//		while(ite.hasNext()){
//			Element element=((com.cea.utils.Type)ite.next()).getUml2Element();
//			TreeItem item = new TreeItem(elemTree, SWT.NONE);
//			item.setText(((NamedElement)element).getName());
//			item.setData(element);
//			item.setImage(PapyrusPlugin.UML_LABEL_PROVIDER.getImage(element));
//		}
//	}
	/**
	 * Returns the selected package.
	 * 
	 * @return behavior Behavior
	 */
	public Package getResult(){
		return selectedPackage;
	}
}
